package com.gitee.fsmxm.ocd.config;

import com.gitee.fsmxm.ocon.DocumentConverter;
import com.gitee.fsmxm.ocon.core.ConvertException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import java.io.File;

/**
 * @author fsmxm
 */
@Configuration
@Slf4j
@Getter
public class DocumentConverterConfig {

    private File tempDir;

    private File transferDir;

    private File downDir;

    private String downUrl;

    @Value("${converter.temp.dir}")
    public void setTempDir(File tempDir) {
        if (!tempDir.exists() && tempDir.mkdirs()) {
            log.info("创建临时目录成功: {}", tempDir.getAbsolutePath());
        }
        this.tempDir = tempDir;
    }

    @Value("${converter.transfer.dir}")
    public void setTransferDir(File transferDir) {
        if (!transferDir.exists() && transferDir.mkdirs()) {
            log.info("创建中转目录成功: {}", transferDir.getAbsolutePath());
        }
        this.transferDir = transferDir;
    }

    @Value("${converter.down.dir}")
    public void setDownDir(File downDir) {
        if (!downDir.exists() && downDir.mkdirs()) {
            log.info("创建下载目录成功: {}", downDir.getAbsolutePath());
        }
        this.downDir = downDir;
    }

    @Value("${converter.down.url}")
    public void setDownUrl(String downUrl) {
        this.downUrl = downUrl;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DocumentConverter documentConverter() throws ConvertException {
        DocumentConverter.Builder builder = DocumentConverter.Builder.builder()
                .poolBegin()
                .appName("word.wps")
                .initProcessNum(2)
                .fatalErrorTime(30000)
                .startAndKillExistProcess(true)
                .poolBegin()
                .appName("excel.wps")
                .initProcessNum(2)
                .startAndKillExistProcess(true)
                .fatalErrorTime(30000)
                .poolBegin()
                .appName("powerpoint.wps")
                .initProcessNum(1)
                .startAndKillExistProcess(true)
                .fatalErrorTime(60000);
        return builder.build();
    }

}
